import { ProTableInstance } from "@/components/ProTable/interface";
import { summationArr, mergeRow } from "@/utils/operationArray";
import { getDictionaryListByCodes } from "@/utils/dict";
import { getSelectChildren } from "@/utils/selectSearch";
import dayjs from "dayjs";

export const a = () => {
  console.log(summationArr, "summationArr", getDictionaryListByCodes, dayjs, mergeRow, getSelectChildren);
};
import http from "@/api";
/**
 * @name 初始化
 */
interface ProTableInstanceOBj {
  [key: string]: ProTableInstance;
}
export const useInit = () => {
  let loading = ref(false);
  // 表格所有的ref
  const tablesRef: ProTableInstanceOBj = {};
  // 表格内容集合
  let tableGather = ref<any>({
    // 主要表格
    dataGrid: {},
    // 底部表格
    bottomDataGrid: []
  });

  // 初始化
  const init = async () => {
    const route = useRoute();
    const { setJson } = route.meta || {};
    loading.value = true;
    const jsonStr = await funcDicFindSetByTitle({ title: setJson });

    const { bottomDataGrid, mainDataGrid } = JSON.parse(jsonStr.value);

    // 主表格交互
    if (mainDataGrid) {
      setDataParams(mainDataGrid, tableGather.value.dataGrid);
    }

    // 底部表格数据
    if (bottomDataGrid?.length) {
      for (let i = 0; i < bottomDataGrid.length; i++) {
        tableGather.value.bottomDataGrid[i] = {};
        setDataParams(bottomDataGrid[i], tableGather.value.bottomDataGrid[i]);
      }
    }
    // setTimeout(() => {
    loading.value = false;
    nextTick(() => {
      tablesRef["ProTable"] && tablesRef["ProTable"].getTableList();
      console.log(tablesRef);
    });
    // }, 1000);
  };
  /**
   * @description: 设置 表格对应参数
   * @param {*} dataGrid
   * @return {*}
   */
  const setDataParams = (params: any, dataGrid?: any) => {
    if (!dataGrid) {
      dataGrid = {};
    }
    Object.keys(params).forEach((key: any) => {
      if (params[key] instanceof Array) {
        dataGrid[key] = [];
        params[key].forEach((item: any, index: number) => {
          dataGrid[key][index] = {};
          setDataParams(item, dataGrid[key][index]);
          return item;
        });
      } else if (typeof params[key] === "string") {
        const v = params[key].split("@");
        if (v.length === 1) {
          dataGrid[key] = params[key];
        } else {
          if (v[0] === "function") {
            // 设置对应方法

            dataGrid[key] = eval(`(${v[1]})`);
            if (key === "dataCallback") {
            }
          } else if (v[0] === "api") {
            setRequestApi(v, dataGrid, key);
          }
        }
      } else if (typeof params[key] === "object") {
        dataGrid[key] = {};

        setDataParams(params[key], dataGrid[key]);
        // dataGrid[key] = params[key];
      } else {
        dataGrid[key] = params[key];
      }
    });
  };

  /**
   * @description: 设置表格的ref
   * @param {any} el
   * @param {string} key ref 标识
   * @return {*}
   */
  const setTableRef = (el: any, key: string) => {
    if (el) {
      tablesRef[key] = el;
    }
  };

  /**
   * @description: 设置请求APi
   * @param {any} params
   * @param {any} dataGrid
   * @param {string} key
   * @return {*}
   */
  const setRequestApi = (v: any, dataGrid: any, key: string) => {
    // 设置api
    const h: any = http;
    // 请求方式
    const model = v[1];
    // 请求地址
    const url: string = v[2];
    const isUrlParams = url.includes("?");
    if (!isUrlParams) {
      dataGrid[key] = (p: any) => h[model](url, p);
    } else {
      dataGrid[key] = (p: any) => h[model](url.replace("?", "/" + p.id), p);
    }
  };

  return {
    tableGather,
    tablesRef,
    loading,
    setTableRef,
    init
  };
};
